import Cookies from 'js-cookie'
import settings from '@/settings.js'

export function getAllCookies() {
  let cookies = document.cookie.split(/;\s/g)
  let cookieObj = {}
  cookies.forEach(function (item) {
    const key = item.split('=')[0]
    cookieObj[key] = item.split('=')[1]
  })
  return cookieObj
}

export function setCookie(key, value, params) {
  return Cookies.set(key, value, { domain: settings.domain, ...params })
}

export function getCookieByKey(key) {
  return Cookies.get(key)
}

export function removeCookieByKey(key, others = null) {
  return Cookies.remove(key, others)
}

export function clearAllCookies() {
  let keys = Object.keys(getAllCookies())
  keys.forEach(key => {
    removeCookieByKey(key)
  })
}
